package com.yellowpage.bigdata.service;

import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;

import com.yellowpage.bigdata.data.NspOnofflogTaRegionDayEntity;
import com.yellowpage.bigdata.dao.NspOnofflogTaRegionDayEntityDao;

public class NspOnofflogTaRegionDayEntityServiceImpl extends BaseServiceImpl implements NspOnofflogTaRegionDayEntityService {
    private NspOnofflogTaRegionDayEntityDao nspOnofflogTaRegionDayEntityDao;

    public void setNspOnofflogTaRegionDayEntityDao(NspOnofflogTaRegionDayEntityDao nspOnofflogTaRegionDayEntityDao) {
        this.nspOnofflogTaRegionDayEntityDao = nspOnofflogTaRegionDayEntityDao;
    }


    /* Smooth generate Method */
    public NspOnofflogTaRegionDayEntity getNspOnofflogTaRegionDayEntity(Long id) {
        return nspOnofflogTaRegionDayEntityDao.get(id);
    }

    public List<NspOnofflogTaRegionDayEntity> getAllNspOnofflogTaRegionDayEntity() {
        return nspOnofflogTaRegionDayEntityDao.getAll();
    }

    public List<Map> queryNspOnofflogTaRegionDayEntityByPage(Map queryMap, PageRender pageRender) {
        List<Map> resultList = getQueryAssistantSQL(queryMap).queryByPage(
                baseDao.getConnection(), queryMap, pageRender);
        return resultList;
    }

    public List<Map> queryAllNspOnofflogTaRegionDayEntity(Map queryMap) {
        List<Map> resultList = getQueryAssistantSQL(queryMap).query(baseDao.getConnection(), queryMap);
        return resultList;
    }

    /**
     * 分页查询
     * */
    @Override
    public List<Map> queryAllNspOnofflogTaRegionDaySumByDate(Map queryMap) {
        QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(
                "SELECT r.SHORT_NAME as REGION_CODE , sum(a.IMEI_NEW_COUNT) as IMEI_NEW_COUNT FROM NSP_ONOFFLOG_TA_REGION_DAY a,OMC_REGION r WHERE a.REGION_CODE=r.REGION_CODE and 1=1 " + getQueryString(queryMap));
        sqlAssistant.setTail("group by r.SHORT_NAME order by r.SHORT_NAME desc");
        List<Map> resultList = sqlAssistant.query(baseDao.getConnection(), queryMap);
        return resultList;
    }

    /**
     * 图表汇总查询
     * */
    private QueryAssistantSQL getQueryAssistantSQL(Map queryMap) {
        QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(
                "SELECT a.LOG_DATE , r.SHORT_NAME as REGION_CODE , a.IMEI_DAY_COUNT , a.ACTIVE_COUNT , a.PASSIVE_COUNT , a.IMEI_ALL_COUNT , a.IMEI_NEW_COUNT , a.CREATE_USER , a.CREATE_TIME , a.NOTE    FROM NSP_ONOFFLOG_TA_REGION_DAY a,OMC_REGION r WHERE a.REGION_CODE=r.REGION_CODE and 1=1 " + getQueryString(queryMap));
        return sqlAssistant;
    }

    private String getQueryString(Map queryMap){
        String startDate = queryMap.get("logDate").equals("") ? "" : " and LOG_DATE >= " + queryMap.get("logDate");
        String endDate = queryMap.get("query_logDate_end").equals("") ? "" : " and LOG_DATE <= " + queryMap.get("query_logDate_end");
        String regions = queryMap.get("regionCode").equals("()") ? "" : " and r.SHORT_NAME in " + queryMap.get("regionCode");
        return startDate + endDate + regions;

    }


    public void saveNspOnofflogTaRegionDayEntity(NspOnofflogTaRegionDayEntity nspOnofflogTaRegionDayEntity) {
        nspOnofflogTaRegionDayEntityDao.save(nspOnofflogTaRegionDayEntity);
    }

    public void updateNspOnofflogTaRegionDayEntity(NspOnofflogTaRegionDayEntity nspOnofflogTaRegionDayEntity) {
        nspOnofflogTaRegionDayEntityDao.update(nspOnofflogTaRegionDayEntity);
    }

    public void delete(List deleteList) {
        nspOnofflogTaRegionDayEntityDao.remove(deleteList);
    }


}	